// pages/me/repwd.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  formSubmit: function (e) {

    var that = this

    var formData = e.detail.value;

    console.log(formData);

    if (formData.old_password == "") {
      wx.showModal({
        title: '请输入旧密码',
      });
      return;
    }

    if (formData.new_password == "") {
      wx.showModal({
        title: '请输入新密码',
      });
      return;
    }

    if (formData.re_password == "") {
      wx.showModal({
        title: '请重复新密码',
      });
      return;
    }

    if (formData.re_password != formData.new_password) {
      wx.showModal({
        title: '两次输入的新密码不一样',
      });
      return;
    }

    wx.showToast({
      title: '正在提交',
      icon: 'loading',
      duration: 2000,
      mask: true,
    });

    http.ApiPost(apiUrl.UrlInfoEdit, formData, function (res) {
      if (res.result.errorCode == 1) {
        wx.hideToast()
        wx.showToast({
          title: '修改成功！',
          icon: 'success',
          duration: 3000
        });
        setTimeout(function () {
          wx.hideToast();
        }, 1000)

        wx.removeStorageSync("token");
        wx.removeStorage({
          key: 'user',
          complete: function (res) {
          }
        })
        wx.reLaunch({
          url: '../login/index'
        })

      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  }
})